package com.tubeonfire.model.admin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeMap;
import java.util.logging.Logger;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.tubeonfire.entity.Channel;

public class ChannelModel {
	private static final Logger log = Logger.getLogger(ChannelModel.class
			.getName());

	private static Objectify ofy;

	private static Cache cache = null;

	private static boolean isRegisted = false;

	private static ObjectifyOpts opts = null;

	private int limit = 12;

	private static String cacheSide = "backEnd_";

	private static String cachePrefix = "channelModel_";

	private static TreeMap<String, String> mapCacheKey = new TreeMap<String, String>();

	private List<Channel> listResult = new ArrayList<Channel>();

	public List<Channel> getListResult() {
		return listResult;
	}

	public void setListResult(List<Channel> listResult) {
		this.listResult = listResult;
	}

	public static void init() {
		if (!isRegisted) {
			isRegisted = true;
			try {
				ObjectifyService.register(Channel.class);
			} catch (Exception e) {
				isRegisted = false;
			}
			try {
				cache = CacheManager.getInstance().getCacheFactory()
						.createCache(Collections.emptyMap());
			} catch (CacheException e) {
				isRegisted = false;
			}
			opts = new ObjectifyOpts().setSessionCache(true);
		}
		ofy = ObjectifyService.begin(opts);
	}

	public ChannelModel() {
		init();
	}

	@SuppressWarnings("unchecked")
	public static void insert(Channel obj) {
		init();
		if (cache != null) {
			String prefix = cachePrefix + "id_" + obj.getId();
			cache.put(prefix, obj);
			clearModelCache();
			com.tubeonfire.model.ChannelModel.clearModelCache();
		}
		ofy.put(obj);
	}

	public static void delete(Channel obj) {
		init();
		String prefix = cachePrefix + "id_" + obj.getId();
		if (cache != null && cache.containsKey(prefix)) {
			cache.remove(prefix);
			clearModelCache();
			com.tubeonfire.model.ChannelModel.clearModelCache();
		}
		ofy.delete(obj);
	}

	@SuppressWarnings("unchecked")
	public static Channel getById(String id) {
		try {
			init();
			Channel obj = new Channel();
			String prefix = cachePrefix + "id_" + id;
			if (cache != null && cache.containsKey(prefix)) {
				obj = (Channel) cache.get(prefix);
			} else {
				try {
					obj = ofy.get(new Key<Channel>(Channel.class, id));
					cache.put(prefix, obj);
				} catch (Exception e) {
					obj = null;
				}
			}
			return obj;
		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public void prepareList() {
		try {
			listResult = new ArrayList<Channel>();
			String prefix = cacheSide + cachePrefix + "list_" + limit;
			mapCacheKey.put(prefix, prefix);
			if (cache != null && cache.containsKey(prefix)) {
				listResult = (ArrayList<Channel>) cache.get(prefix);
			} else {

				Query<Channel> q = ofy.query(Channel.class).limit(limit)
						.order("-bumpPoint");
				for (Channel channel : q) {
					listResult.add(channel);
				}
				if (listResult.size() > 0) {
					cache.put(prefix, listResult);
				}
			}
		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace();
			listResult = new ArrayList<Channel>();
		}
	}

	public static void clearModelCache() {
		init();
		try {
			if (mapCacheKey != null && mapCacheKey.size() > 0) {
				for (String key : mapCacheKey.keySet()) {
					cache.remove(key);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
